package com.maycurobj.service.impl;

import com.maycurobj.entity.bpm.PersonnelInfoDTO;
import com.maycurobj.entity.bpm.SuperiorInfoEntity;
import com.maycurobj.entity.oa.OASuperiorEntity;
import com.maycurobj.repository.bpm.*;
import com.maycurobj.repository.oa.ObtainShangjiRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 取bpm、OA员工、上级等信息
 */
@Service
@Slf4j
public class OAService {
    @Autowired
    private ObtainShangjiRepository obtainShangjiRepository;

    @Autowired
    private PersonnelInfoMapper personnelInfoMapper;

    @Autowired
    private SuperiorUpdateMapper SuperiorUpdateMapper;

    /**
     * 取OA员工、上级信息，工号不允许为空
     * @return
     */
    public List<OASuperiorEntity> getSuperiors(){
        return obtainShangjiRepository.getSuperior();
    }

    /**
     * 取BPM员工信息，工号不允许为空、员工状态等于开启
     * @return
     */
    public List<PersonnelInfoDTO> getPersonnelInfo(){
        return personnelInfoMapper.getPersonnelInfo();
    }

    public void updateSuperior(List<SuperiorInfoEntity> list){

        for (SuperiorInfoEntity list1:list){
            try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String formattedDate = format.format(date);

            Integer str =  SuperiorUpdateMapper.updateSuperior(list1.getId(),formattedDate,list1.getId(),list1.getBpmdepartmentId(),list1.getBpmshangjiid());
            log.info("更新成功--->成功人员姓名：{}，工号：{},影响行数：{}",list1.getBpmname(),list1.getBpmemployeeNo(),str);

            }catch (Exception e){
                log.info("更新失败--->失败人员姓名：{}，工号：{},失败原因：{}",list1.getBpmname(),list1.getBpmemployeeNo(),e.getMessage());
            }
        }
    }

}
